package com.block.channel.flow.channel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channel.flow.IChannelReportService;
import com.block.channel.model.result.ReprotFlowResult;
import com.block.common.bean.CommonParam;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelBaseInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TuiAChannelReportService implements IChannelReportService {

    private static Logger log = LogFactory.getCommLog();

    private String requestUrl = "https://activity.tuia.cn/log/effect/v2";
    private String secretKey = "B93DCB8803BDF70D461B1FF44CC20756";

    @Override
    public ReprotFlowResult registerActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        ReprotFlowResult flowResult = new ReprotFlowResult();
        flowResult.setSuccess(false);

        JSONObject headObject = commonParam.getHeadObject();
        String a_oId = headObject.getString("a_oId");
        if (StrUtil.isEmpty(a_oId)){
            flowResult.setCode("-2");
            flowResult.setMessage("a_oId参数为空，不做处理");
            return flowResult;
        }

        String reqIp = commonParam.getReqIp();

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("a_oId", a_oId);
        bizMap.put("advertKey", secretKey);
        bizMap.put("subType", "3");
        bizMap.put("ip", reqIp);

        log.info("【推啊信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .form(bizMap)
                .execute().body();
        log.info("【推啊信息流上报】【注册】【开始结束】【result】{}", response);

        JSONObject jsonObject = JSONObject.parseObject(response);
        String code = jsonObject.getString("record");
        String message = jsonObject.getString("redesc");

        flowResult.setCode(String.valueOf(code));
        flowResult.setMessage(message);
        flowResult.setParamInfo(JSON.toJSONString(bizMap));
        flowResult.setResultInfo(response);
        if (StrUtil.equals(code, "0000000")){
            flowResult.setSuccess(true);
        }else{
            flowResult.setSuccess(false);
        }
        return flowResult;
    }
    @Override
    public ReprotFlowResult sourceActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }
    @Override
    public ReprotFlowResult downloadActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }

    @Override
    public ReprotFlowResult payBuyActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }


}
